<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/5/17
 * Time: 10:27
 * 未登录状态下进行快速续费
 */
namespace app\home\controller;

use think\Controller;
use think\Session;

class Quick extends Controller {

    protected $model;   // 实例化模型
    /**
     * 初始化
     */
    public function _initialize()
    {
        $this->model = model('Quick');
    }

    /**
     * 快速续费页面
     */
    public function index()
    {
        // 获取用户的授权
        $redirect_url = 'http://'.$_SERVER['HTTP_HOST'].'/home/Quick/index';   // 授权回调地址
        $ifAuth = model('Base')->getAuthUserOpenid($redirect_url);
        // 生成微信扫码接口所需的参数
        $redirect_url = 'http://'.$_SERVER['HTTP_HOST'].'/home/Quick/index.html';
        $scan_param = model('Base')->getScanParamB($redirect_url);
        $this->assign('scan_param',$scan_param);
        return view();
    }

    /**
     * 验证卡片
     */
    public function checkCard()
    {
        $check = $this->model->checkCardM();
        return $check;
    }

    /**
     * 续费支付页面
     */
    public function renew()
    {
        // 验证卡片信息
        $check = $this->model->checkCardM();
        if($check['code']){
            $this->error();
            exit;
        }
        $this->assign('iccid',$check['card'][0]['iccid']);
        // 获取卡片归属的直接用户信息
        $client = model('Personal')->getUserInfoM($check['card'][0]['cid']);
        $client = [
            'id'=>$client['id'],
            'agent_id'=>$client['agent_id'],
            'setpkg'=>$client['setpkg'],
            'setbag'=>$client['setbag'],
        ];
        Session::set('S_N_USER_INFO',$client);   // session设置未登录时续费卡片的归属用户信息
        // 根据验证的卡片信息获取卡片的套餐信息
        $card = model('Base')->getCardPackageM($check['card'][0]['pid'],$client);
        if($card['code']){   // 套餐信息错误
            $this->error();
            exit;
        }
        $this->assign('card',$card['package']);
        return view();
    }
    
    /**
     * 确认支付
     */
    public function confirmRenew()
    {
        $result = $this->model->confirmRenewM();
        return $result;
    }

    /**
     * 支付回调统一地址，不需要登录
     */
    public function wxNotifyUrl()
    {
        $result = $this->model->handleWxInfo();
    }

}